package com.ohuang.jiaxiaotong.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.ohuang.jiaxiaotong.entity.ChatMsg;

import java.util.List;
import com.ohuang.jiaxiaotong.R;
public class ChatRvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private LayoutInflater mLayoutInflater;
    private Context mContext;
    private List<ChatMsg> mData;
    private String username;

    public ChatRvAdapter(Context mContext, List<ChatMsg> mData,String username) {
        this.mLayoutInflater = LayoutInflater.from(mContext);
        this.mContext = mContext;
        this.mData = mData;
        this.username=username;
    }

    public void setmData(List<ChatMsg> mData) {
        this.mData = mData;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 0) {
            return new ChatRvAdapter.MyTVHolder(mLayoutInflater.inflate(R.layout.rv_chat0_item, parent,false));
        } else {
            return new ChatRvAdapter.MyTVHolder1(mLayoutInflater.inflate(R.layout.rv_chat1_item, parent,false));
        }

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ChatRvAdapter.MyTVHolder) {
            ((MyTVHolder) holder).mTextView.setText(mData.get(position).getContent());
            ImageView imageView = ((MyTVHolder) holder).imageView;
            Glide.with(mContext).load(mData.get(position).getSendIcon())
                    .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                    .into(imageView);
            if (position<=0){
                ((MyTVHolder) holder).txt_time.setText(mData.get(position).getSendTime());
            }else if ((mData.get(position).getTimes()-mData.get(position-1).getTimes())>300000){
                ((MyTVHolder) holder).txt_time.setText(mData.get(position).getSendTime());
            }else {
                ((MyTVHolder) holder).txt_time.setVisibility(View.GONE);
            }
        }
        if (holder instanceof ChatRvAdapter.MyTVHolder1) {
            ((MyTVHolder1) holder).mTextView.setText(mData.get(position).getContent());
            ImageView imageView = ((MyTVHolder1) holder).imageView;
            Glide.with(mContext).load(mData.get(position).getSendIcon())
                    .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                    .into(imageView);
            if (position<=0){
                ((MyTVHolder1) holder).txt_time.setText(mData.get(position).getSendTime());
            }else if ((mData.get(position).getTimes()-mData.get(position-1).getTimes())>300000){
                ((MyTVHolder1) holder).txt_time.setText(mData.get(position).getSendTime());
            }else {
                ((MyTVHolder1) holder).txt_time.setVisibility(View.GONE);
            }
        }

    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    class MyTVHolder extends RecyclerView.ViewHolder {
        TextView mTextView,txt_time;
        ImageView imageView;


        MyTVHolder(View itemView) {
            super(itemView);
            mTextView = (TextView) itemView.findViewById(R.id.txt_msg_rv_chat0);
            imageView = itemView.findViewById(R.id.img_icon_rv_chat0);
            txt_time=itemView.findViewById(R.id.txt_time_rv_chat0);
        }


    }

    class MyTVHolder1 extends RecyclerView.ViewHolder {
        TextView mTextView,txt_time;
        ImageView imageView;

        MyTVHolder1(View itemView) {
            super(itemView);
            mTextView = (TextView) itemView.findViewById(R.id.txt_msg_rv_chat1);
            imageView = itemView.findViewById(R.id.img_icon_rv_chat1);
            txt_time=itemView.findViewById(R.id.txt_time_rv_chat1);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (username.equals(mData.get(position).getSendName())){
            return 0;
        }else {
            return 1;
        }

    }
}
